package org.example.utils;

import org.example.domain.vo.LoginUserVo;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityUtil {

    public static Authentication getAuthentication(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return authentication;
    }

    /**
     * 获取用户
     * @return
     */
    public static LoginUserVo getLoginUser(){
        return (LoginUserVo) getAuthentication().getPrincipal();
    }
    /*public static LoginUserVO getLoginUser() {
        return (LoginUserVO) getAuthentication().getPrincipal();
    }*/
    /**
     * 获取用户id
     * @return
     */
    public static Long getUserId(){
        LoginUserVo loginUser = getLoginUser();
        return loginUser.getId();
    }

    /**
     * 获取用户名
     * @return
     */
    public static String getUserName(){
        LoginUserVo loginUser = getLoginUser();
        return loginUser.getUser().getUsername();
    }
}
